package com.jinglin.web.rabbitListener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jinglin.component.RedisComponent;
import com.jinglin.entity.constants.Constants;
import com.jinglin.entity.dto.VideoPlayInfoDto;
import com.jinglin.entity.po.VideoInfo;
import com.jinglin.entity.po.VideoInfoFilePost;
import com.jinglin.entity.po.VideoPlayHistory;
import com.jinglin.service.VideoInfoFilePostService;
import com.jinglin.service.VideoInfoService;
import com.jinglin.service.VideoPlayHistoryService;
import com.jinglin.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class videoListener {
    @Autowired
    private VideoInfoFilePostService videoInfoFilePostService;
    @Autowired
    private VideoInfoService videoInfoService;
    @Autowired
    private RedisComponent redisComponent;
    @Autowired
    private VideoPlayHistoryService videoPlayHistoryService;

   // 异步转码任务放到这里来做
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "video.transfer.queue1"),
            exchange = @Exchange(value = "jinglinOnline.videoTransfer.direct"),
            key = "transfer"))
    public void videoTransfer(VideoInfoFilePost videoInfoFilePost){
        // 如果没有信息，他就不会拿到，所以不存在videoInfoFilePost为空的情况
        log.info("监听到转码文件信息：{}",videoInfoFilePost);
        try{
            videoInfoFilePostService.transferVideoFile(videoInfoFilePost);
        }catch (Exception e){
            log.error("获取转码文件信息失败",e);
        }
    }

    // 异步转码任务放到这里来做
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "video.play.queue1"),
            exchange = @Exchange(value = "jinglinOnline.videoPlay.direct"),
            key = "play"))
    public void videoPlay(VideoPlayInfoDto videoPlayInfoDto){
        // 如果没有信息，他就不会拿到，所以不存在videoInfoFilePost为空的情况
        log.info("监听到播放文件信息：{}",videoPlayInfoDto);
        try {
            // 更新视频浏览量
            videoInfoService.addReadCount(videoPlayInfoDto.getVideoId());
            // 更新视频上传播放时间
            LambdaUpdateWrapper<VideoInfo> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(VideoInfo::getVideoId,videoPlayInfoDto.getVideoId());
            wrapper.set(VideoInfo::getLastPlayTime, LocalDateTime.now());
            videoInfoService.update(wrapper);
            // 如果处于登录状态的播放，则记录历史
            if (!StringTools.isEmpty(videoPlayInfoDto.getUserId())){
                VideoPlayHistory videoPlayHistory = new VideoPlayHistory();
                BeanUtils.copyProperties(videoPlayInfoDto,videoPlayHistory);
                videoPlayHistory.setLastUpdateTime(LocalDateTime.now());
                videoPlayHistoryService.saveVideoHistory(videoPlayHistory);
            }

            // 记录当天24小时浏览量。
            redisComponent.recordVideoPlayCount(videoPlayInfoDto.getVideoId());


            // 更新es播放数量()
            // TODO 不是很懂ES的语法，这里有一个bug，对于我来说有点难度，因此不在ES中拿这个数据videoPlayCount。

/*
             esSearchComponent.updateDocCount(videoPlayInfoDto.getVideoId(), SearchOrderTypeEnum.VIDEO_PLAY.getField(), 1);
*/

        } catch (Exception e) {
            log.error("获取转码文件队列信息失败",e);
        }
    }

}
